#include "missionmodel.h"

#include <QFileInfo>
#include <QFont>

MissionModel::MissionModel(MissionList *list, QObject *parent)
  : QAbstractTableModel(parent)
{
  missionList = list;

  headers << "源文件"
          << "目标文件"
          << "状态";
}

QVariant MissionModel::headerData(int section, Qt::Orientation orientation, int role) const
{


  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    return headers[section];
  return QVariant();
}

int MissionModel::rowCount(const QModelIndex &parent) const
{
  if (parent.isValid())
    return 0;
  return missionList->size();

}

int MissionModel::columnCount(const QModelIndex &parent) const
{
  if (parent.isValid())
    return 0;
  return headers.size();

}

QVariant MissionModel::data(const QModelIndex &index, int role) const
{
  if (!index.isValid())
    return QVariant();

  Mission m = missionList->at(index.row());

  if (role == Qt::DisplayRole) {
    if (index.column() == 0) {
      QFileInfo info(m.source);
      return info.fileName();
    }
    if (index.column() == 1) {
      QFileInfo info(m.target);
      return info.fileName();
    }
    if (index.column() == 2) {
      QMap<Mission::Status, QString> s_map {
        {Mission::CANCELED, "CANCELED"},
        {Mission::DONE, "DONE"},
        {Mission::ERROR, "ERROR"},
        {Mission::WAITING, "WAITING"},
        {Mission::WORKING, "WORKING"}
      };
      return s_map[m.status];
    }
  }

  if (role == Qt::ToolTipRole) {
    if (index.column() == 0)
      return m.source;
    if (index.column() == 1)
      return m.target;
  }

  if (role == Qt::FontRole) {
    QFont font("Heiti SC", 12);
    return font;
  }

  return QVariant();
}
